package DBase.Players;

import DBase.Board;
//import DBase.Coordinate;


/**
 * 
 * @author Elya & Dana
 */
public abstract class Player {
    
    private String name;
    private Board myBoard;
    /*package friendly*/ Board [] rivalBoards;
    private int playerTurnInGame;
    private boolean playerActive;
    
    /**
     * Player Constructor
     */
    public Player(){
        
        playerActive=true;
        myBoard = new Board();       
    }
    
    /**
     * Restarts the players board and rivals boards
     */
    public void restart(){
     
        
        playerActive=true;
        myBoard.restart();
        int[] boardNum = new int[rivalBoards.length];
        for(int i=0;i<rivalBoards.length;i++){
            boardNum[i] =  rivalBoards[i].getPlayerNumber();
            rivalBoards[i] = new Board();
            rivalBoards[i].setPlayerNumber( boardNum[i]);
        }
    }
    
    /**
     * Get rival player index based on board number
     * @param boardNum the rival board number
     * @return rival index
     */
    public int getRivalPlayerIndex(int boardNum){
        
        int index = -1;
        for(int i=0;i<rivalBoards.length;i++)
            if(rivalBoards[i].getPlayerNumber() == boardNum)
                index=i;
        return index;
    } 
    
    /**
     * Set the number of rival boards for this player
     * @param numPlayers
     * @param numberOfThisPlayer
     */
    public void setNumOfRivalBoards(int numPlayers,int numberOfThisPlayer) {
        
        int i,j=0;
       
        rivalBoards = new Board[numPlayers-1];
        for (i=0;i<numPlayers-1; i++){
            rivalBoards[i] = new Board();
            if(numberOfThisPlayer != i){
                rivalBoards[i].setPlayerNumber(j);
                j++;
            }
            else{
                j++;
                rivalBoards[i].setPlayerNumber(j);
                j++;
            }
        }
    }
    
    /**
     * Get rival board based on player number
     * @param PlayerNumber
     * @return Board
     */
    public Board getRivalBoard(int PlayerNumber){
        
        Board board = new Board();
        
        for(int i=0;i<rivalBoards.length;i++){
            if(rivalBoards[i].getPlayerNumber() == PlayerNumber)
                board=rivalBoards[i];
        }
        return board;
    }
    
    /**
     * Check if a player is still active in the game
     * @return true if active, false otherwise
     */
    public boolean checkIsPlayerActive(){
    
        if(myBoard.getNumOfShipsActive()==0 || playerActive == false){
            playerActive=false;
            myBoard.setIsBoardActive(false);
            return false;
        }
        else return true;
    }
    
    /**
     * Get if player is active
     * @return true of false
     */
    public boolean getIsPlayerActive() {
        return playerActive;
    }

    /**
     * Set if player is active
     * @param playerActive true or false
     */
    public void setPlayerActive(boolean playerActive) {
        this.playerActive = playerActive;
    }
    
    /**
     * Get all the rival boards as an array
     * @return rivalBoards array
     */
    public Board [] getRivalBoardsArr(){
        return rivalBoards; 
    }
    
    /**
     * Set this players board
     * @param myBoard this players board
     */
    public void setMyBoard(Board myBoard) {
        this.myBoard = myBoard;
    }

    /**
     * Set player name(cannot be empty)
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }
    
    /**
     * Get player name
     * @return name
     */
    public String getName() {
        return name;
    }
    
    /**
     * Get players board
     * @return myBoard
     */
    public Board getMyBoard() {
        return myBoard;
    }

    /**
     * Set this players turn in the game
     * @param playerTurnInGame
     */
    public void setPlayerTurnInGame(int playerTurnInGame) {
        this.playerTurnInGame = playerTurnInGame;
    }      
    /**
     * Get this players turn in the game
     * @return playerTurnInGame
     */
    public int getPlayerTurnInGame() {
        return playerTurnInGame;
    }
     
}
